﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Collections.ObjectModel;



namespace UniCT.LinkShareWpf.Interface 
{
    
    [DataContract]
    [Serializable]
    
    // the KnownTypeAttribute attribute allows information about derived types to be included in the data contract. If this mechanism is not used, a 


    [KnownType(typeof(ContentInfoTVS))]
    [XmlInclude(typeof(ContentInfoTVS))]
    public class ContentInfo : IDataErrorInfo, INotifyPropertyChanged
    {

        #region attributes
        [DataMember]
        String director;
        [DataMember]
        String title;
        [DataMember]
        String storyline;
        [DataMember]
        EnumGenre genre;
        [DataMember]
        EnumLanguage language;
        [DataMember]
        EnumLanguage? subTitleLanguage;
        [DataMember]
        UInt32 year;
        [DataMember]
        UInt32 duration;
        [DataMember]
        CastCollection cast;
        [DataMember]
        String coverName;
        #endregion

        public ContentInfo()
        {

            director = "director";
            title = "title11111111111111111111111111111";
            storyline = "storyline";
            language = EnumLanguage.Cinese;
            genre = EnumGenre.Criminale;
            subTitleLanguage = EnumLanguage.Arabo;
            year = 1999;
            duration = 123;
            cast = new CastCollection();
            coverName = "Inception.jpg";
          
  
          
        }

        public ContentInfo(ContentInfo copy)
        {
            this.Cast = copy.Cast;
            this.CoverName = copy.CoverName;
            this.Director = copy.Director;
            this.Duration = copy.Duration;
            this.Genre = copy.Genre;
            this.Language = copy.Language;
            this.Storyline = copy.Storyline;
            this.SubTitleLanguage = copy.SubTitleLanguage;
            this.Title = copy.Title;
            this.Year = copy.Year;
         

        }

        public String Director
        {
            get { return this.director; }
            set
            {
                if (value != this.director)
                {
                    this.director = value;
                    this.OnPropertyChanged("Director");
                }
            }
        }


        public String Title
        {
            get { return title; }
            set
            {
                if (value != this.title)
                {
                    this.title = value;
                    this.OnPropertyChanged("Title");
                }
            }
        }

        public string Storyline
        {
            get { return this.storyline; }
            set
            {
                if (value != this.storyline)
                {
                    this.storyline = value;
                    this.OnPropertyChanged("Storyline");
                }
            }
        }

        public EnumGenre Genre
        {
            get { return this.genre; }
            set
            {
                if (value != this.genre)
                {
                    this.genre = value;
                    this.OnPropertyChanged("Genre");
                }
            }
       }

        public EnumLanguage Language
        {
            get { return this.language; }
            set
            {
                if (value != this.language)
                {
                    this.language = value;
                    this.OnPropertyChanged("Language");
                }
            }
       }

        public EnumLanguage? SubTitleLanguage
        {
            get { return this.subTitleLanguage; }
            set
            {
                if (value != this.subTitleLanguage)
                {
                    this.subTitleLanguage = value;
                    this.OnPropertyChanged("SubTitleLanguage");
                }
            }
        }

        public UInt32 Year
        {
            get { return this.year; }
            set
            {
                if (value > 0 && value != this.year)
                {
                    this.year = value;
                    this.OnPropertyChanged("Year");
                }
            }
        }

        public UInt32 Duration
        {
            get { return this.duration; }
            set
            {
                if (value > 0 && value != this.duration)
                {
                    this.duration = value;
                    this.OnPropertyChanged("Duration");
                }
            }
        }

        public String CoverName
        {
            get { return this.coverName; }
            set
            {
                if (value != this.coverName)
                {
                    this.coverName = value;
                    this.OnPropertyChanged("CoverName");
                }
            }
        }

        [XmlArray("Cast")]
        [XmlArrayItem("Actor")]
        public CastCollection Cast
        {
            get { return this.cast; }
            set
            {
                if (value != this.cast)
                {
                    this.cast = value;
                    this.OnPropertyChanged("Cast");
                    Console.WriteLine("Ho settato il CASTTTTTTTTT!!!");
                }
            }
        }

        public override string ToString()
        {
            string actors = String.Empty;
            if (cast != null)
                foreach (string actor in cast)
                    actors += actor + ",";
            
            return string.Format("ContentInfo: {0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", director, title, storyline, genre, language, subTitleLanguage, year, duration,actors,coverName);
        }
     

        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string propName]
        {
            get
            {
                if (propName == "Director")
                {
                    if (this.Director.Equals(string.Empty))
                    {
                        return "Il nome del regista non può essere vuoto.";
                    }
                }

                if (propName == "Title")
                {
                    if (this.Director.Equals(string.Empty))
                    {
                        return "Il titolo non può essere vuoto.";
                    }
                }

                if (propName == "Storyline")
                {
                    if (this.Storyline.Equals(string.Empty))
                    {
                        return "La trama non può essere vuota.";
                    }
                }

                if (propName == "Year")
                {
                    if (this.Year < 1900 || this.Year > DateTime.Today.Year)
                    {
                        return "L'anno inserito non è valido.";
                    }
                }

                return null;
            }
        }

        #endregion
        #region INotifyPropertyChanged Support

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }


        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        
    }

    public enum EnumGenre
    {
       
        
        Animazione,
        Avventura,
        Azione,
        Biografia,
        Commedia,
        Criminale,
        Documentale,
        Drammatico,
        Family,
        Fantasy,
        Guerra,
        Horror,
        Musical,
        Mistery,
        Romantico,
        Scientifico,
        Sportivo,
        Storico,
        Thriller,
        Western

    }

    public enum EnumLanguage
    {

        Arabo,
        Cinese,
        Francese,
        Giapponese,
        Inglese,
        Italiano,
        Spagnolo,
        Tedesco

    }
}
